package com.bpmn.common.holder;

import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.bpmn.common.constant.CommonConstants;

/**
 * 多租户上下文 Holder
 *
 * @author rain
 */
public class TenantContextHolder {

    /**
     * 当前租户编号
     */
    private static final ThreadLocal<String> TENANT_ID = new TransmittableThreadLocal<>();

    /**
     * 是否忽略租户
     */
    private static final ThreadLocal<Boolean> IGNORE = new TransmittableThreadLocal<>();

    /**
     * 获得租户编号
     *
     * @return 租户编号
     */
    public static String getTenantId() {
        return StrUtil.isBlank(TENANT_ID.get()) ? CommonConstants.TENANT_ID_DEFAULT : TENANT_ID.get();
    }

    public static void setTenantId(String tenantId) {
        TENANT_ID.set(tenantId);
    }

    public static void clear() {
        TENANT_ID.remove();
        IGNORE.remove();
    }

}
